#!/bin/sh

#
# This script outputs the number of seconds since 1/1/1970
#
seconds_gnu_epoch=`date +'%s'`

# This is the equation that works for the Gregorian calendar for years from
# 1582 through 9999.  If you need to calculate something else, you'll need 
# a new equation.
#Date_days=(Year*365)+(Year/4)-(Year/100)+(Year/400)+(Month*306001/10000)+(Day)
# If the month is greater than 2, add 1 to the month.  Otherwise, for Jan/Feb,
# add 13 to the month and subtract 1 from the year.  The following is the
# equation for Jan 1, 1970.
epoch_days=$(((1969*365)+(1969/4)-(1969/100)+(1969/400)+(14*306001/10000)+1))

hour=`date +'%k'`
minute=`date +'%M'`
second=`date +'%S'`

day=`date +'%d'`
month=`date +'%m'`
year=`date +'%Y'`

if [ $month -gt 2 ]
then
  month=$(($month+1))
else
  month=$(($month+13))
  year=$(($year-1))
fi

today_days=$((($year*365)+($year/4)-($year/100)+($year/400)+($month*306001/10000)+$day))
days_since_epoch=$(($today_days-$epoch_days))
seconds_since_epoch=`echo "($days_since_epoch*86400)+($hour*3600)+($minute*60)+$second" | bc`
# There will likely be a difference in the following two values.  The GNU
# time in seconds is calculated based on GMT instead of the timezone you
# are currently in.  Mine, being pacific time is currently calculating with
# a difference of exactly 8 hr.
echo $seconds_since_epoch
